home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3.iso / chapter5 / exdirect.c < prev    next >
C/C++ Source or Header  |  1996-04-27  |  10KB  |  286 lines

  1. #include <windows.h>  
  2. #include <stdio.h>
  3. #include "exdirect.h"
  4. #include "sqlext.h"  
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "MyApp";
  20. LPCTSTR lpszTitle   = "SQLExecDirect()"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106.  
  107. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  108. {
  109. static HENV  hEnv  = NULL;
  110. static HDBC  hDBC  = NULL;
  111. static HWND  hList = NULL;
  112. UCHAR szSqlStr[] = "Select dept_id, dept_name, dept_head_id From department";
  113.  
  114.    switch( uMsg )
  115.    {
  116.       case WM_CREATE :
  117.               {
  118.                  RETCODE retCode;
  119.  
  120.                  // Allocate Environment and Connection.
  121.                  //.....................................
  122.                  SQLAllocEnv( &hEnv );
  123.                  SQLAllocConnect( hEnv, &hDBC );
  124.  
  125.                  // Connect to the data source
  126.                  //...........................
  127.                  retCode = SQLConnect( hDBC, 
  128.                              "Test Data Source", SQL_NTS,
  129.                              "DBA",              SQL_NTS,  
  130.                              "SQL",              SQL_NTS );
  131.  
  132.                  if ( retCode == SQL_SUCCESS )
  133.                  {
  134.                     int nTabSize = 60;
  135.  
  136.                     hList = CreateWindow( "LISTBOX", "",    
  137.                                LBS_NOTIFY | LBS_USETABSTOPS |
  138.                                LBS_NOINTEGRALHEIGHT  | 
  139.                                WS_BORDER  | WS_CHILD | 
  140.                                WS_VISIBLE | WS_VSCROLL, 
  141.                                0, 0, 0, 0, 
  142.                                hWnd, (HMENU)101,
  143.                                hInst, NULL );
  144.                     
  145.                     SendMessage( hList, LB_SETTABSTOPS, 
  146.                                  1, (LPARAM)&nTabSize );
  147.                  }
  148.               }
  149.               break;
  150.  
  151.       case WM_SIZE :
  152.               MoveWindow( hList, 0, 0, LOWORD( lParam ), 
  153.                                        HIWORD( lParam ), TRUE );
  154.               break; 
  155.               
  156.       case WM_COMMAND :
  157.               switch( LOWORD( wParam ) )
  158.               {
  159.                  case IDM_TEST :
  160.                         {
  161.                            HSTMT   hStmt;
  162.                            UCHAR   szDeptId    [32];
  163.                            UCHAR   szDeptName  [32];
  164.                            UCHAR   szDeptHeadId[32];
  165.                            CHAR    szLBStr[128];
  166.                            SDWORD  dwTypeLen;
  167.                            RETCODE retCode;
  168.                            int     i = 0;
  169.  
  170.                            // Allocate a statement handle for the query.
  171.                            // ..........................................
  172.                            SQLAllocStmt( hDBC, &hStmt );
  173.  
  174.                            // Call SQLExecDirect() to 
  175.                            // compile and execute the query.
  176.                            // ..............................
  177.                            retCode = SQLExecDirect( hStmt, szSqlStr,
  178.                                                     sizeof(szSqlStr) );
  179.  
  180.                            if( retCode == SQL_SUCCESS || 
  181.                                retCode == SQL_SUCCESS_WITH_INFO )
  182.                            {
  183.                               // Bind a column to the dept_id, 
  184.                               // dept_name, and dept_head_id 
  185.                               // columns in the result set.
  186.                               // .............................
  187.                               SQLBindCol( hStmt, 1, SQL_C_CHAR, 
  188.                                  szDeptId, sizeof( szDeptId ), 
  189.                                  &dwTypeLen );
  190.  
  191.                               SQLBindCol( hStmt, 2, SQL_C_CHAR, 
  192.                                  szDeptName, sizeof( szDeptName ),
  193.                                  &dwTypeLen );
  194.  
  195.                               SQLBindCol( hStmt, 3, SQL_C_CHAR, 
  196.                                  szDeptHeadId, sizeof( szDeptHeadId ),
  197.                                   &dwTypeLen );
  198.  
  199.                               // Fetch all the records and
  200.                               // put them in the list box.
  201.                               //..........................
  202.                               retCode = SQLFetch( hStmt );
  203.  
  204.                               while ( retCode == SQL_SUCCESS &&
  205.                                       i < 5)
  206.                               {
  207.                                  szLBStr[0] = '\0';
  208.                                  strcat( szLBStr, (char*)szDeptId );
  209.                                  strcat( szLBStr, "\t" );
  210.                                  strcat( szLBStr, (char*)szDeptName );
  211.                                  strcat( szLBStr, "\t" );
  212.                                  strcat( szLBStr, (char*)szDeptHeadId );
  213.  
  214.                                  SendMessage( hList, LB_ADDSTRING, 
  215.                                               0, (LPARAM)szLBStr );
  216.  
  217.                                  retCode = SQLFetch( hStmt );
  218.                               }
  219.  
  220.                               // If 5 records were fetched, cancel 
  221.                               // the query with SQL Cancel().
  222.                               // .................................
  223.                               if( i == 5 )
  224.                                  SQLCancel( hStmt );
  225.                            }
  226.  
  227.                            // Free the statement handle.
  228.                            // ..........................
  229.                            SQLFreeStmt( hStmt, SQL_DROP );
  230.                         }
  231.                         break;
  232.  
  233.                  case IDM_ABOUT :
  234.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  235.                         break;
  236.  
  237.                  case IDM_EXIT :
  238.                         DestroyWindow( hWnd );
  239.                         break;
  240.               }
  241.               break;
  242.       
  243.       case WM_DESTROY :
  244.               PostQuitMessage(0);
  245.  
  246.               // Disconnect Environment.
  247.               //........................
  248.               SQLDisconnect( hDBC );
  249.  
  250.               // Free Connection and Environment.
  251.               //.................................
  252.               SQLFreeConnect( hDBC );
  253.               SQLFreeEnv( hEnv );
  254.               break;
  255.  
  256.       default :
  257.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  258.    }
  259.  
  260.    return( 0L );
  261. }
  262.  
  263.  
  264. LRESULT CALLBACK About( HWND hDlg,           
  265.                         UINT message,        
  266.                         WPARAM wParam,       
  267.                         LPARAM lParam)
  268. {
  269.    switch (message) 
  270.    {
  271.        case WM_INITDIALOG: 
  272.                return (TRUE);
  273.  
  274.        case WM_COMMAND:                              
  275.                if (   LOWORD(wParam) == IDOK         
  276.                    || LOWORD(wParam) == IDCANCEL)    
  277.                {
  278.                        EndDialog(hDlg, TRUE);        
  279.                        return (TRUE);
  280.                }
  281.                break;
  282.    }
  283.  
  284.    return (FALSE); 
  285. }
  286.